package wx.core
{
	import flash.display.DisplayObjectContainer;
	import flash.utils.Dictionary;
	
	import wx.controls.View;
	import wx.motion.Tween;

	public class ViewManager
	{
		
		private static var _dict:Dictionary = new Dictionary();
		private static var current:String = "";
		
		public static var top:DisplayObjectContainer;
		
		public static function addView(id:String,view:Class):void{
			_dict[id] = view;
		}
		
		public static function removeView(id:String):void{
			_dict[id] = null;
			delete _dict[id];
		}
		
		public static function show(id:String,hasTransition:Boolean = false):void{
			if(_dict.hasOwnProperty(id) && id != current){	
				var newview:View = (new _dict[id]) as View;
				if(current!=""){
					var oldview:View = top.getChildAt(0) as View;
					if(hasTransition){
						var nx:Number = SettingCache.mainWidth;
						newview.x = nx;
						top.addChild(newview);
						new Tween(top,0.4,{x:-nx},{onComplete:function():void{
							top.removeChild(oldview);
						}});						
					}else{
						top.removeChild(oldview);
						top.addChild(newview);
					}
					
				}else{
					top.addChild(newview);
				}				
				current = id;
			}
		}
		
	}
}